﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using QLHD.Models;
using System.Globalization;

namespace QLHD.Account
{
    public partial class GroupSystems : SystemAdmin
    {
        public class Block
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public List<string> BlockNames { get; set; }

            public Block(int id, string name, params string[] names)
            {
                ID = id;
                Name = name;
                BlockNames = new List<string>();
                foreach (var item in names)
                {
                    BlockNames.Add(item);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (IsPostBack)
                return;

            QLHD2013Entities db = new QLHD2013Entities();

            List<QLHD.Models.Object> obj = new List<QLHD.Models.Object>();
            obj = db.Objects.ToList();
            List<Block> blocks = new List<Block>();
            //List<PhongBan> pb;
            foreach (QLHD.Models.Object item in obj)
            {
                blocks.Add(new Block(item.ID, item.Name, "None", "View", "Edit", "Add"));
            }
            rptDummy.DataSource = blocks;
            rptDummy.DataBind();
        }

        protected void rptDummy_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                QLHD2013Entities db = new QLHD2013Entities();
                int groupID = int.Parse(Request.QueryString["GroupID"].ToString());
                HiddenField hidInput = (HiddenField)e.Item.FindControl("ID");
                int nitemID = int.Parse(hidInput.Value);
                var perValue = db.GroupPermissions.Where(q => q.GroupID == groupID && q.ObjectID == nitemID).Select(q => q.PermissionValue).FirstOrDefault();
                RadioButtonList rdlst = (RadioButtonList)e.Item.FindControl("rdlist");
                if (rdlst != null)
                {
                    rdlst.SelectedValue = perValue.ToString();
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            QLHD2013Entities db = new QLHD2013Entities();
            int groupID = int.Parse(Request.QueryString["GroupID"].ToString());
            List<QLHD.Models.Object> obj = db.Objects.ToList();
            //foreach (GroupPermission itemGU in obj)
            {
                foreach (RepeaterItem item in rptDummy.Items)
                {
                    var rdbList = item.FindControl("rdlist") as RadioButtonList;
                    int permision = int.Parse(rdbList.SelectedValue);

                    var idInput = item.FindControl("ID") as HiddenField;
                    int ObjectID = int.Parse(idInput.Value);

                   Models.GroupPermission objPU = db.GroupPermissions.Where(q => q.GroupID == groupID && q.ObjectID == ObjectID).FirstOrDefault();

                    if (objPU != null)
                    {
                        objPU.ObjectID = ObjectID;
                        objPU.GroupID = groupID;
                        objPU.PermissionValue = permision;
                    }
                    else
                    {
                        objPU = new Models.GroupPermission();
                        objPU.ObjectID = ObjectID;
                        objPU.GroupID = groupID;
                        objPU.PermissionValue = permision;
                        db.AddToGroupPermissions(objPU);
                    }
                    db.SaveChanges();
                }
            }
            Response.Redirect("GroupPermissionList.aspx");
        }
    }
}